package com.gmall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseCategory;
import com.gmall.model.product.dto.BaseCategoryDTO;
import com.gmall.model.product.dto.Category123IdNameDTO;
import com.gmall.product.mapper.BaseCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryService {


    @Autowired
    BaseCategoryMapper baseCategoryMapper;

    public Result<List<BaseCategoryDTO>> findBaseCatgoryDTOTree(){
        List<BaseCategoryDTO> baseCategoryDTOList = baseCategoryMapper.selectBaseCatgoryDTOTree();
        return Result.ok(baseCategoryDTOList);
    }

    public Result<List<BaseCategoryDTO>> findBaseCatgoryDTOTreeStream(){
        List<BaseCategory> baseCategories = baseCategoryMapper.selectList(Wrappers.emptyWrapper());
        //流
        /**
         * 1	图书、音像、电子书刊
         * 2	手机
         * 3	家用电器
         * 4	数码
         * 5	家居家装
         * 6	电脑办公
         * 7	厨具
         * 8	个护化妆
         * 9	服饰内衣
         * 10	钟表
         * 11	鞋靴
         * 12	母婴
         * 13	礼品箱包
         * 14	食品饮料、保健食品
         * 15	珠宝
         * 16	维修保养
         * 17	运动鞋包
         */
        List<BaseCategoryDTO> dtoList = baseCategories.stream()
                .filter(baseCategory->baseCategory.getParentID().equals(0L))
                .map((baseCategory -> {
                    BaseCategoryDTO dto = new BaseCategoryDTO();
                    dto.setId(baseCategory.getId());
                    dto.setName(baseCategory.getName());
                    dto.setLevel(baseCategory.getLevel());
                    dto.setChildren(getChildren(baseCategories,baseCategory.getId()));
                    return dto;
                })).collect(Collectors.toList());

        return Result.ok(dtoList);
    }

    public List<BaseCategoryDTO> getChildren(List<BaseCategory> baseCategoryList,Long parentId){

        List<BaseCategoryDTO> collect =
                baseCategoryList.stream().filter(baseCategory -> baseCategory.getParentID().equals(parentId))
                .map(baseCategory -> {
                    BaseCategoryDTO dto = new BaseCategoryDTO();
                    dto.setId(baseCategory.getId());
                    dto.setName(baseCategory.getName());
                    dto.setLevel(baseCategory.getLevel());
                    dto.setChildren(getChildren(baseCategoryList,baseCategory.getId()));
                    return dto;
                }).collect(Collectors.toList());
        return collect.size()==0?null:collect;
    }

    public static void main(String[] args) {
        //Long -128 ~127
        //== 比较的是地址
        Long a = 1000L;
        Long b = 1000L;
        System.out.println(a==b);
    }

    public Result<List<BaseCategory>> getCategory1() {
        //1级 parent_id = 0
        //select * from base_category where parent_id = 0;
        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, 0);
        List<BaseCategory> list = baseCategoryMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    public Result<List<BaseCategory>> getCategory2( Long category1Id) {
        //1级 parent_id = 0
        //select * from base_category where parent_id = #{category1Id};
        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, category1Id);
        List<BaseCategory> list = baseCategoryMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    public Result<List<BaseCategory>> getCategory3( Long category2Id) {
        //1级 parent_id = 0
        //select * from base_category where parent_id = #{category1Id};
        QueryWrapper<BaseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategory::getParentID, category2Id);
        List<BaseCategory> list = baseCategoryMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    public Result<Category123IdNameDTO> getCategory123IdNameDTO(Long category3Id) {

        Category123IdNameDTO category123IdNameDTO
                = baseCategoryMapper.selectCategory123IdName(category3Id);

        return Result.ok(category123IdNameDTO);
    }
}
